﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using WeChat.JsSDK;
//类库BLL中无System.Web命名空间，所以该方法不适合放入类库
namespace WeChat.Common2
{
    public class ReturnTextXml
    {
        /// <summary>
        /// 响应事件，返回xml格式文本消息
        /// </summary>
        /// <param name="ToUserName">接收方帐号（收到的OpenID）</param>
        /// <param name="FromUserName">开发者微信号</param>
        /// <returns></returns>
        public static string returnText(string ToUserName, string FromUserName, string content)
        {
            XmlDocument doc = new XmlDocument();
            //MapPath命名空间默认page类，如果不在aspx.cs中使用需要使用server命名空间
            doc.Load(HttpContext.Current.Server.MapPath("User.xml"));
            XmlNodeList xml = doc.DocumentElement.ChildNodes;
            foreach (XmlNode xn in xml)
            {
                if (xn.Name == "ToUserName")
                {
                    xn.AppendChild(doc.CreateCDataSection(ToUserName));
                    continue;
                }
                if (xn.Name == "FromUserName")
                {
                    xn.AppendChild(doc.CreateCDataSection(FromUserName));
                    continue;
                }
                if (xn.Name == "CreateTime")
                {
                    xn.InnerText = GetSdkParams.getTimestamp();
                    continue;
                }
                if (xn.Name == "MsgType")
                {
                    xn.AppendChild(doc.CreateCDataSection("text"));
                    continue;
                }
                if (xn.Name == "Content")
                {
                    xn.AppendChild(doc.CreateCDataSection(content));
                }
            }
            //返回拼装好的xml字符串
            return doc.InnerXml;
        }
    }
}
